{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入所需模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from lxml.html import fromstring\n",
    "import time\n",
    "from random import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding=utf-8\n",
    "from selenium import webdriver\n",
    "import time\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.get(\"https://www.baidu.com\")    # 打开百度浏览器\n",
    "wd.find_element_by_id(\"kw\").send_keys(\"selenium\")   # 定位输入框并输入关键字\n",
    "wd.find_element_by_id(\"su\").click()   #点击[百度一下]搜索\n",
    "time.sleep(3)   #等待3秒\n",
    "wd.quit()   #关闭浏览器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-5-8ae4025e7ff4>:19: DeprecationWarning: use options instead of chrome_options\n",
      "  driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.desired_capabilities import DesiredCapabilities\n",
    "\n",
    "\n",
    "#caps=dict()\n",
    "#caps[\"pageLoadStrategy\"] = \"none\"   # Do not wait for full page load\n",
    "\n",
    "opts = webdriver.ChromeOptions()\n",
    "opts.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错\n",
    "opts.add_argument('window-size=1920x3000') #指定浏览器分辨率\n",
    "opts.add_argument('--disable-gpu') #谷歌文档提到需要加上一这个属性来规避bug\n",
    "opts.add_argument('--hide-scrollbars') #隐藏滚动条, 应对些特殊页面\n",
    "#opts.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度\n",
    "#opts.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败\n",
    "# opts.binary_location = \"C:\\portable\\PortableApps\\IronPortable\\App\\Iron\\chrome.exe\"\n",
    "# opts.binary_location = \"C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe\" #\"H:\\_coding_\\Gitee\\InternetNewMedia\\CapstonePrj2016\\chromedriver.exe\"  \n",
    "\n",
    "\n",
    "driver = webdriver.Chrome( chrome_options = opts) #desired_capabilities=caps,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.get(\"https://mp.weixin.qq.com\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.自动化登录（需扫码）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1自动化账号密码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "payload =  {\"account\": \"1337494356@qq.com\", \"password\": \"ZWWY2020210\"}\n",
    "# payload =  {\"account\": \"NFUHacks@163.com\", \"password\": \"NFU706947580\"}\n",
    "# 切换为账号密码登录\n",
    "driver.find_element_by_xpath('//div[@class=\"login__type__container login__type__container__scan\"]/a').click()\n",
    "#element.get_attribute(\"innerHTML\")\n",
    "#element.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 清空账号input\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"account\"]').send_keys(payload['account'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 清空密码input\n",
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//input[@name=\"password\"]').send_keys(payload['password'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//div[@class=\"login_btn_panel\"]/a').click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2 处理验证码(若没弹出省略此步骤)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "verifycode_image_url = driver.find_element_by_xpath('//form[@class=\"login_form\"]//div[@class=\"verifycode\"]/img').get_attribute(\"src\")\n",
    "verifycode_image_url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.f36fa744d907c703f9281383d0d34084.2592000.1623431198.282335-20147076'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 百度API\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=9auo2pBlmStXYAkd0t3V60N8&client_secret=PP5aCfUh21sB6mPZvoE7FlGVw9GgZnGA'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "#     print(response.json())\n",
    "    \n",
    "    access_token = response.json()[\"access_token\"]\n",
    "access_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "params = {\n",
    "    \"url\":verifycode_image_url\n",
    "}\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' IRGA'"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "verifycode=response.json()[\"words_result\"][0][\"words\"]\n",
    "verifycode"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "driver.find_element_by_xpath('//form[@class=\"login_form\"]//div[@class=\"verifycode\"]//input').send_keys(verifycode)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 找选单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 展开\n",
    "element = driver.find_element_by_xpath('//a[@id=\"m_open\"]')\n",
    "element.click()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击图文素材\n",
    "#element = driver.find_element_by_xpath('//*[@id=\"js_mp_sidemenu_pop\"]/div[2]/div/div/ul/li[2]/ul/li[1]/ul/li[1]/a')\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-layout__side_pop__wrp\"]//ul[@class=\"weui-desktop-menu weui-desktop-menu_global\"]//a')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击+\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-panel\"]//div[@class=\"new-creation__menu-item\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 新建图文消息\n",
    "# element = driver.find_element_by_xpath('//*[@id=\"js_main\"]/div[3]/div[2]/div/div/div/div[1]/div/div[1]/div[2]/ul/li[1]/a')\n",
    "# element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['CDwindow-847914296F9D6A856931CA07CA76913E', 'CDwindow-81595AEB26BB51858A291EF683B7516C']\n"
     ]
    }
   ],
   "source": [
    "# 检查窗口信息\n",
    "print (driver.window_handles)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-18-6c6d5ce6602d>:1: DeprecationWarning: use driver.switch_to.window instead\n",
      "  driver.switch_to_window(driver.window_handles[1])\n"
     ]
    }
   ],
   "source": [
    "driver.switch_to_window(driver.window_handles[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "## 点击超链接\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-head__inner js_header_inner\"]//div[@class=\"media_list_box_inner\"]//ul[@class=\"tpl_list\"]/li[@class=\"tpl_item\"][1]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击选择其他公众号\n",
    "element = driver.find_element_by_xpath('//div[@class=\"weui-desktop-dialog__bd\"]//div[@class=\"weui-desktop-form__control-group\"]//div[@class=\"weui-desktop-form__controls\"]//div[@class=\"weui-desktop-btn_wrp weui-desktop-link-btn weui-desktop-link\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 点击刺猬工社\n",
    "driver.find_element_by_xpath('//*[@id=\"vue_app\"]/div[2]/div[1]/div/div[2]/div[2]/form[1]/div[3]/div/div/div/div/span/input').clear()\n",
    "driver.find_element_by_xpath('//*[@id=\"vue_app\"]/div[2]/div[1]/div/div[2]/div[2]/form[1]/div[3]/div/div/div/div/span/input').send_keys(\"刺猬公社\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "from time import sleep\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.action_chains import ActionChains"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<div class=\"weui-desktop-icon weui-desktop-icon__search weui-desktop-icon__small\" style=\"width: 20px; height: 20px;\"><!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!----> <!---->     <svg viewBox=\"0 0 24 24\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>MP/Icon/Search</title> <g id=\"MP/Icon/Search\" stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"><path d=\"M5.78025253,5.78248558 C8.51392257,3.04881554 12.9460774,3.04881554 15.6797475,5.78248558 C18.1730922,8.27583028 18.3922898,12.1821488 16.3373403,14.9239313 L20.6294949,19.2175144 L19.2152814,20.631728 L14.922508,16.3389663 C12.180685,18.394566 8.27384272,18.1755707 5.78025253,15.6819805 C3.04658249,12.9483105 3.04658249,8.51615562 5.78025253,5.78248558 Z M6.8409127,6.84314575 C4.6930291,8.99102935 4.6930291,12.4734367 6.8409127,14.6213203 C8.98879631,16.7692039 12.4712037,16.7692039 14.6190873,14.6213203 C16.7669709,12.4734367 16.7669709,8.99102935 14.6190873,6.84314575 C12.4712037,4.69526215 8.98879631,4.69526215 6.8409127,6.84314575 Z\" id=\"形状\"></path></g></svg> <!----> <!----> <!----> <!----> <!----></div>\n"
     ]
    }
   ],
   "source": [
    "## 点击放大镜\n",
    "element = driver.find_element_by_xpath('//button[@class=\"weui-desktop-icon-btn weui-desktop-search__btn\"]')\n",
    "main_content = element.get_attribute('innerHTML')\n",
    "print(main_content)\n",
    "ActionChains(driver).move_to_element(element).click().perform()  \n",
    "element.click()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 收集公众号相关信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "element = driver.find_element_by_xpath('//div[@class=\"link_dialog_panel\"]//div[@class=\"weui-desktop-form__control-group\"]//div[@class=\"weui-desktop-vm_primary\"]')\n",
    "element.click()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 418]\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "# 跳转上限\n",
    "l_e = driver.find_elements_by_xpath('//label[@class=\"weui-desktop-pagination__num\"]')\n",
    "l_e_int  = [int(x.text) for x in l_e] \n",
    "print (l_e_int)\n",
    "print (l_e_int[0]==l_e_int[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286, 287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325, 326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338, 339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351, 352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364, 365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377, 378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390, 391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418]\n"
     ]
    }
   ],
   "source": [
    "pages = list(range(l_e_int[0],l_e_int[-1]+1 ))\n",
    "#print(pages[0:2])\n",
    "pages = list(range(1,l_e_int[-1]+1 ))\n",
    "print(pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "pages = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [],
   "source": [
    "# global varialbes \n",
    "# 循环 遍历\n",
    "html_raw = dict()\n",
    "main_content =\"\"\n",
    "element = None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_pages (pages):\n",
    "    for p in pages:\n",
    "        print (p,end='\\t')\n",
    "\n",
    "        跳转_input = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/input')\n",
    "        跳转_a = driver.find_element_by_xpath('//span[@class=\"weui-desktop-pagination__form\"]/a')\n",
    "        跳转_input.clear()\n",
    "        跳转_input.send_keys(p)\n",
    "        跳转_a.click()\n",
    "\n",
    "        time.sleep(45+120*random())\n",
    "\n",
    "        element = driver.find_element_by_xpath('//div[@class=\"inner_link_article_list\"]')\n",
    "        main_content = element.get_attribute('innerHTML')\n",
    "        #print(main_content)\n",
    "        html_raw[p] = main_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2\t3\t4\t5\t6\t7\t8\t9\t10\t11\t12\t13\t14\t15\t16\t17\t18\t19\t20\t21\t22\t23\t24\t25\t26\t27\t28\t29\t30\t31\t32\t33\t34\t35\t36\t37\t38\t39\t40\t41\t42\t43\t44\t45\t46\t47\t48\t49\t50\t51\t52\t53\t54\t55\t56\t57\t58\t59\t60\t"
     ]
    }
   ],
   "source": [
    "process_pages (pages)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>&lt;div class=\"weui-desktop-radio-group\"&gt;&lt;label c...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                       html_snippets\n",
       "1  <div class=\"weui-desktop-radio-group\"><label c..."
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame([html_raw]).T\n",
    "df.columns = [\"html_snippets\"]\n",
    "df.loc[0:1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Stored 'html_raw' (dict)\n"
     ]
    }
   ],
   "source": [
    "%store html_raw\n",
    "import pickle \n",
    "filehandler = open(\"html_raw\", 'wb') \n",
    "pickle.dump(html_raw, filehandler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "59\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>html_snippets</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>&lt;div class=\"weui-desktop-radio-group\"&gt;&lt;label c...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        html_snippets\n",
       "12  <div class=\"weui-desktop-radio-group\"><label c..."
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df.duplicated()  默认所有列，无重复记录  【duplicated()函数】判断是否有重复项\n",
    "df_out = df[~df.duplicated()]\n",
    "print (len(df_out))\n",
    "df[df.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[12]"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "try_again = list(df[df.duplicated()].index)\n",
    "print(try_again)\n",
    "try_again = try_again + list (set(pages).difference(set(df.index.values)))\n",
    "try_again\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入“公众号”参数\n",
    "公众号 = \"刺猬公社\"\n",
    "# 指定内容输出的位置\n",
    "fn = { \"output\" : { \"公众号_htm_snippets\": \"data_raw_src_/公众号_htm_snippets_{公众号}.tsv\",\n",
    "                    \"公众号_df\": \"data_raw_src_/公众号_df_{公众号}.tsv\",\n",
    "                    \"公众号_xlsx\": \"公众号_url_{公众号}.xlsx\" } \\\n",
    "      }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # 暂存档\n",
    "# filename = fn [\"output\"] [\"公众号_htm_snippets\"] \n",
    "# df_out.to_csv(filename.format(公众号=公众号), sep=\"\\t\", encoding=\"utf8\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19,20,20,20,20,19,20,19,20,20,20,20,20,20,20,20,20,20,20,18,18,19,20,20,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,18,20,20,20,20,20,20,20,20,20,20,21,20,20,20,20,20,20,"
     ]
    }
   ],
   "source": [
    "from requests_html import HTMLSession\n",
    "def get_content(link):\n",
    "    session = HTMLSession()\n",
    "    r = session.get(url=link)\n",
    "    content_xpath_1 = '//*[@id=\"js_content\"]//span/text()'\n",
    "    content_xpath_2 = '//*[@id=\"js_content\"]//p/text()'\n",
    "    content_1 = ''.join(r.html.xpath(content_xpath_1))\n",
    "    content_2 = ''.join(r.html.xpath(content_xpath_2))\n",
    "    return content_1 + content_2\n",
    "\n",
    "def parse_html_snippets(_snippet_):\n",
    "    root = fromstring(_snippet_) \n",
    "    title = [x.text for x in root.xpath('//div[@class=\"inner_link_article_title\"]//span[2]')]\n",
    "    create_time = [x.text for x in root.xpath('//div[@class=\"inner_link_article_date\"]')]\n",
    "    link = [x for x in root.xpath('//a/@href')]\n",
    "    content_text = [get_content(x) for x in link]\n",
    "    _df_ = pd.DataFrame({\"title\":title, \"create_time\": create_time, \"link\":link, \"content_text\":content_text})\n",
    "    return(_df_)\n",
    "    \n",
    "l_df = []\n",
    "for p in pages:\n",
    "    _df_ = parse_html_snippets(df.loc[p,\"html_snippets\"])\n",
    "    print (len(_df_), end=\",\")\n",
    "    l_df.append(_df_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>终于，云村要上市了</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>专注于通过培养“云村”社区扩大用户群体及用户参与度，为长远盈利能力铺平道路。这也就是网易云的...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>电商内卷：“天天”都是618</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>618起得更早了，阿里京东更焦虑了挨了182亿罚款之后，阿里比以往起得更早了。把物流推上IP...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>刺猬公社招聘作者，正职、见习快来快来！</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>近期，我们做出了以下一些报道作品：1、内容产业南移调查：离开北京，去杭州收割一个时代（后台回...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>微信视频号直播新增重要入口；2021全球电竞运动领袖峰会议程揭晓；亚马逊宣布以84.5亿美元...</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博是谁在监控我的618？5月26日，微信视频号更新新功能。在视频号和公众号绑定的前提...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>“卑微文学”在抖音横空出世</td>\n",
       "      <td>2021-05-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>不想聊就不聊了呗，摆什么臭架子，觉得自己很帅、很牛吗？作者 | 松露来源 | 新榜（ID：n...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1184</th>\n",
       "      <td>白玉兰奖公布入围名单；当当成立“公司权益保护部”；Instagram将推出短视频功能 | 猬报</td>\n",
       "      <td>2020-07-18</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：豆瓣#投其所好#你玩过B站出品的游戏吗你最看好谁？看小说，上淘宝？庆渝年最新集这是想钻...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1185</th>\n",
       "      <td>“国家队”在知乎破圈</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>“知乎在走一条很艰难的路，要优雅地面对更广大受众，展现出人民群众喜闻乐见的BCDEF面，同时...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1186</th>\n",
       "      <td>美国名流在Twitter上集体撒钱了？</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>究竟是哪里出了问题，让Twitter平台最高等级的账户被轻而易举地入侵？Twitter用户近...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1187</th>\n",
       "      <td>闯入主流世界的小众文化：为什么说“破圈”是把双刃剑？</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>“破圈是一把双刃剑，有可能打开一个新市场和带来新的客户和流量，进而带动整个平台的进一步发展，...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1188</th>\n",
       "      <td>低风险地区电影院7月20日开放营业；D站创始人被批捕；趣头条针对3.15晚会报道发道歉声明 ...</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博总有一个适合你所以A-Z站都有吗？道歉速度挺快会创造出更多洗脑神曲吗字节这是每个领...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1189 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  title create_time  \\\n",
       "0                                             终于，云村要上市了  2021-05-28   \n",
       "1                                        电商内卷：“天天”都是618  2021-05-28   \n",
       "2                                   刺猬公社招聘作者，正职、见习快来快来！  2021-05-28   \n",
       "3     微信视频号直播新增重要入口；2021全球电竞运动领袖峰会议程揭晓；亚马逊宣布以84.5亿美元...  2021-05-28   \n",
       "4                                         “卑微文学”在抖音横空出世  2021-05-27   \n",
       "...                                                 ...         ...   \n",
       "1184    白玉兰奖公布入围名单；当当成立“公司权益保护部”；Instagram将推出短视频功能 | 猬报  2020-07-18   \n",
       "1185                                         “国家队”在知乎破圈  2020-07-17   \n",
       "1186                                美国名流在Twitter上集体撒钱了？  2020-07-17   \n",
       "1187                         闯入主流世界的小众文化：为什么说“破圈”是把双刃剑？  2020-07-17   \n",
       "1188  低风险地区电影院7月20日开放营业；D站创始人被批捕；趣头条针对3.15晚会报道发道歉声明 ...  2020-07-17   \n",
       "\n",
       "                                                   link  \\\n",
       "0     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "2     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "3     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "4     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "...                                                 ...   \n",
       "1184  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1185  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1186  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1187  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1188  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "\n",
       "                                           content_text  \n",
       "0     专注于通过培养“云村”社区扩大用户群体及用户参与度，为长远盈利能力铺平道路。这也就是网易云的...  \n",
       "1     618起得更早了，阿里京东更焦虑了挨了182亿罚款之后，阿里比以往起得更早了。把物流推上IP...  \n",
       "2     近期，我们做出了以下一些报道作品：1、内容产业南移调查：离开北京，去杭州收割一个时代（后台回...  \n",
       "3     图源：微博是谁在监控我的618？5月26日，微信视频号更新新功能。在视频号和公众号绑定的前提...  \n",
       "4     不想聊就不聊了呗，摆什么臭架子，觉得自己很帅、很牛吗？作者 | 松露来源 | 新榜（ID：n...  \n",
       "...                                                 ...  \n",
       "1184  图源：豆瓣#投其所好#你玩过B站出品的游戏吗你最看好谁？看小说，上淘宝？庆渝年最新集这是想钻...  \n",
       "1185  “知乎在走一条很艰难的路，要优雅地面对更广大受众，展现出人民群众喜闻乐见的BCDEF面，同时...  \n",
       "1186  究竟是哪里出了问题，让Twitter平台最高等级的账户被轻而易举地入侵？Twitter用户近...  \n",
       "1187  “破圈是一把双刃剑，有可能打开一个新市场和带来新的客户和流量，进而带动整个平台的进一步发展，...  \n",
       "1188  图源：微博总有一个适合你所以A-Z站都有吗？道歉速度挺快会创造出更多洗脑神曲吗字节这是每个领...  \n",
       "\n",
       "[1189 rows x 4 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_url_out = pd.concat(l_df).reset_index(drop=True)\n",
    "df_url_out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ==&mid=2247592564&idx=1&sn=c306da696acd47131b8b4540b4f12c15&chksm=c1446dd2f633e4c404f50b332e5a13cc4385a638a67f34c3689cc2ebb220067cb8d9c9fb7405#rd'"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 试验公众号文章链接是否正确\n",
    "df_url_out.loc[0].link"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>217</th>\n",
       "      <td>“最快乐的有钱人”丁磊这次拿出10亿奖励自家学员</td>\n",
       "      <td>2021-03-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>谁能拿到丁磊的10亿“梦想基金”？ 2020年夏天，网易二度上市。自公司上市20年来，丁磊在...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>218</th>\n",
       "      <td>独立游戏谢绝腾讯招安</td>\n",
       "      <td>2021-03-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>武昭含腾讯游戏依旧是印钞机，但在过去的一年里，各路敌手和这个中国乃至世界游戏巨无霸的距离正在...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>219</th>\n",
       "      <td>离开字节跳动的人</td>\n",
       "      <td>2021-03-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>字节扁平的组织架构，让中层格外疲惫。这里比其他大厂更快，人人像热锅上的蚂蚁。“在字节，别把自...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220</th>\n",
       "      <td>B站登陆港交所；微信iOS版更新；《哥斯拉大战金刚》内地首周末斩获约6140万票房丨猬报</td>\n",
       "      <td>2021-03-30</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博一点点，再来一点点B站正式登陆港交所北京时间3月29日，哔哩哔哩正式在香港联合交易...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>221</th>\n",
       "      <td>美篇，不止是老年版Ins</td>\n",
       "      <td>2021-03-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>当你还在担心老年人被互联网落下，美篇里的老年“产品经理”们正用互联网思维指点江山。提起美篇，...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>222</th>\n",
       "      <td>9块9就能买盗版网课，谁来解决在线教育版权难题</td>\n",
       "      <td>2021-03-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>这是一个多输的局面。作者 | 刘鑫  九磅是法律系的大学生，曾参与过卖盗版在线网络课程。在问...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>223</th>\n",
       "      <td>没有人再在游戏里结婚</td>\n",
       "      <td>2021-03-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>作者 | 张宇婷在我国互联网产业二十多年的发展历程里，“网恋”这件事几乎从未缺席。比如在19...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>224</th>\n",
       "      <td>辛巴复出7小时直播卖货15亿；《断背山》编剧麦克穆特瑞去世；10部电影押注五一档丨猬报</td>\n",
       "      <td>2021-03-29</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博哈哈，可恶辛巴复出7小时直播卖货15亿战绩斐然《断背山》编剧麦克穆特瑞去世R.I....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>225</th>\n",
       "      <td>揭秘THE9云演唱会幕后故事：应援棒被炒到999元，粉丝深夜等偶像下班送信</td>\n",
       "      <td>2021-03-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>明星是所有人的明星，偶像却只是你一个人的偶像。3月26日早上6点，嘎嘎和老公相继起床，简单梳...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>226</th>\n",
       "      <td>知乎上市首日破发，豆瓣你怎么看</td>\n",
       "      <td>2021-03-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>豆瓣的知识付费产品“豆瓣时间”，也因过于小众、文艺，没有激起太多水花。作者 | 李唐编辑 |...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>227</th>\n",
       "      <td>从WE明凯到ESTAR诺言，电竞业变成明星孵化器</td>\n",
       "      <td>2021-03-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>2013年，老WE队员受邀参加录制湖南经视《越策越开心》，当时的厂长还叫诺言，刚满20岁，处...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>228</th>\n",
       "      <td>辛巴辛有志开播；知乎上市首日开盘破发；斗鱼因其主播直播间唱歌侵权被判赔1000元 | 猬报</td>\n",
       "      <td>2021-03-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博启动失败辛巴辛有志开播2021首播知乎上市首日开盘破发支棱起来啊斗鱼因其主播直播间...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>229</th>\n",
       "      <td>小宇宙诞生一周年记，中文播客走入“爆发”岁月</td>\n",
       "      <td>2021-03-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>恰饭、做社交之前，先让播客和用户彼此看见。作者 | 王馨婉石    灿许多年之后，面对不断“...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>230</th>\n",
       "      <td>我和罗永浩直播这一年</td>\n",
       "      <td>2021-03-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>我们试图通过其身边人的讲诉，还原罗永浩以及他直播一年的幕后故事。来源 | 公众号“交个朋友观...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>231</th>\n",
       "      <td>​李少红新剧《大宋宫词》，与历史剧的终结</td>\n",
       "      <td>2021-03-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>我们熟悉的历史剧评价标准正在被模糊。作者 | 谢明宏编辑 | 李春晖 “父皇自幼喜欢三弟。”...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>232</th>\n",
       "      <td>B站回应将百度写进公司资料表；公安机关下架636款教育类App和网课平台；知乎IPO定价9....</td>\n",
       "      <td>2021-03-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博无熊在意B站回应将百度写进公司资料表真尴尬公安机关下架636款教育类App和网课平...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>233</th>\n",
       "      <td>斗鱼的新“三板斧”</td>\n",
       "      <td>2021-03-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>在游戏直播竞争转为生态战的大背景下，斗鱼已经准备好了自身的三板斧。作者 | 陈彬 斗鱼仍在保...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>234</th>\n",
       "      <td>一场求婚直播千万人围观，还卖了800万的纸巾？</td>\n",
       "      <td>2021-03-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>足以可见，不少人已经对殷世航的直播“上头”了。作者 | 松露殷世航，中文互联网新晋流量网红，...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>235</th>\n",
       "      <td>卧底00后交友群，我人傻了</td>\n",
       "      <td>2021-03-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>看到这里，想必大家多少会觉得自己有点老了……现在的 00后在玩什么？你可能一无所知： 他们会...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>236</th>\n",
       "      <td>陌陌公布2020年第四季度及全年财报；脉脉发布《人才流动与迁徙2021》报告；淘宝特价版宣布...</td>\n",
       "      <td>2021-03-26</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博掩耳盗铃手法非遗传承人陌陌公布2020年第四季度及全年财报陌陌好像福利很好脉脉发布...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                 title create_time  \\\n",
       "217                           “最快乐的有钱人”丁磊这次拿出10亿奖励自家学员  2021-03-30   \n",
       "218                                         独立游戏谢绝腾讯招安  2021-03-30   \n",
       "219                                           离开字节跳动的人  2021-03-30   \n",
       "220       B站登陆港交所；微信iOS版更新；《哥斯拉大战金刚》内地首周末斩获约6140万票房丨猬报  2021-03-30   \n",
       "221                                       美篇，不止是老年版Ins  2021-03-29   \n",
       "222                            9块9就能买盗版网课，谁来解决在线教育版权难题  2021-03-29   \n",
       "223                                         没有人再在游戏里结婚  2021-03-29   \n",
       "224        辛巴复出7小时直播卖货15亿；《断背山》编剧麦克穆特瑞去世；10部电影押注五一档丨猬报  2021-03-29   \n",
       "225              揭秘THE9云演唱会幕后故事：应援棒被炒到999元，粉丝深夜等偶像下班送信  2021-03-28   \n",
       "226                                    知乎上市首日破发，豆瓣你怎么看  2021-03-28   \n",
       "227                           从WE明凯到ESTAR诺言，电竞业变成明星孵化器  2021-03-28   \n",
       "228      辛巴辛有志开播；知乎上市首日开盘破发；斗鱼因其主播直播间唱歌侵权被判赔1000元 | 猬报  2021-03-28   \n",
       "229                             小宇宙诞生一周年记，中文播客走入“爆发”岁月  2021-03-27   \n",
       "230                                         我和罗永浩直播这一年  2021-03-27   \n",
       "231                               ​李少红新剧《大宋宫词》，与历史剧的终结  2021-03-27   \n",
       "232  B站回应将百度写进公司资料表；公安机关下架636款教育类App和网课平台；知乎IPO定价9....  2021-03-27   \n",
       "233                                          斗鱼的新“三板斧”  2021-03-26   \n",
       "234                            一场求婚直播千万人围观，还卖了800万的纸巾？  2021-03-26   \n",
       "235                                      卧底00后交友群，我人傻了  2021-03-26   \n",
       "236  陌陌公布2020年第四季度及全年财报；脉脉发布《人才流动与迁徙2021》报告；淘宝特价版宣布...  2021-03-26   \n",
       "\n",
       "                                                  link  \\\n",
       "217  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "218  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "219  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "220  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "221  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "222  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "223  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "224  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "225  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "226  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "227  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "228  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "229  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "230  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "231  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "232  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "233  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "234  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "235  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "236  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "\n",
       "                                          content_text  \n",
       "217  谁能拿到丁磊的10亿“梦想基金”？ 2020年夏天，网易二度上市。自公司上市20年来，丁磊在...  \n",
       "218  武昭含腾讯游戏依旧是印钞机，但在过去的一年里，各路敌手和这个中国乃至世界游戏巨无霸的距离正在...  \n",
       "219  字节扁平的组织架构，让中层格外疲惫。这里比其他大厂更快，人人像热锅上的蚂蚁。“在字节，别把自...  \n",
       "220  图源：微博一点点，再来一点点B站正式登陆港交所北京时间3月29日，哔哩哔哩正式在香港联合交易...  \n",
       "221  当你还在担心老年人被互联网落下，美篇里的老年“产品经理”们正用互联网思维指点江山。提起美篇，...  \n",
       "222  这是一个多输的局面。作者 | 刘鑫  九磅是法律系的大学生，曾参与过卖盗版在线网络课程。在问...  \n",
       "223  作者 | 张宇婷在我国互联网产业二十多年的发展历程里，“网恋”这件事几乎从未缺席。比如在19...  \n",
       "224  图源：微博哈哈，可恶辛巴复出7小时直播卖货15亿战绩斐然《断背山》编剧麦克穆特瑞去世R.I....  \n",
       "225  明星是所有人的明星，偶像却只是你一个人的偶像。3月26日早上6点，嘎嘎和老公相继起床，简单梳...  \n",
       "226  豆瓣的知识付费产品“豆瓣时间”，也因过于小众、文艺，没有激起太多水花。作者 | 李唐编辑 |...  \n",
       "227  2013年，老WE队员受邀参加录制湖南经视《越策越开心》，当时的厂长还叫诺言，刚满20岁，处...  \n",
       "228  图源：微博启动失败辛巴辛有志开播2021首播知乎上市首日开盘破发支棱起来啊斗鱼因其主播直播间...  \n",
       "229  恰饭、做社交之前，先让播客和用户彼此看见。作者 | 王馨婉石    灿许多年之后，面对不断“...  \n",
       "230  我们试图通过其身边人的讲诉，还原罗永浩以及他直播一年的幕后故事。来源 | 公众号“交个朋友观...  \n",
       "231  我们熟悉的历史剧评价标准正在被模糊。作者 | 谢明宏编辑 | 李春晖 “父皇自幼喜欢三弟。”...  \n",
       "232  图源：微博无熊在意B站回应将百度写进公司资料表真尴尬公安机关下架636款教育类App和网课平...  \n",
       "233  在游戏直播竞争转为生态战的大背景下，斗鱼已经准备好了自身的三板斧。作者 | 陈彬 斗鱼仍在保...  \n",
       "234  足以可见，不少人已经对殷世航的直播“上头”了。作者 | 松露殷世航，中文互联网新晋流量网红，...  \n",
       "235  看到这里，想必大家多少会觉得自己有点老了……现在的 00后在玩什么？你可能一无所知： 他们会...  \n",
       "236  图源：微博掩耳盗铃手法非遗传承人陌陌公布2020年第四季度及全年财报陌陌好像福利很好脉脉发布...  "
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 找出重复项\n",
    "df_url_out[df_url_out.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>title</th>\n",
       "      <th>create_time</th>\n",
       "      <th>link</th>\n",
       "      <th>content_text</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>终于，云村要上市了</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>专注于通过培养“云村”社区扩大用户群体及用户参与度，为长远盈利能力铺平道路。这也就是网易云的...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>电商内卷：“天天”都是618</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>618起得更早了，阿里京东更焦虑了挨了182亿罚款之后，阿里比以往起得更早了。把物流推上IP...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>刺猬公社招聘作者，正职、见习快来快来！</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>近期，我们做出了以下一些报道作品：1、内容产业南移调查：离开北京，去杭州收割一个时代（后台回...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>微信视频号直播新增重要入口；2021全球电竞运动领袖峰会议程揭晓；亚马逊宣布以84.5亿美元...</td>\n",
       "      <td>2021-05-28</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博是谁在监控我的618？5月26日，微信视频号更新新功能。在视频号和公众号绑定的前提...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>“卑微文学”在抖音横空出世</td>\n",
       "      <td>2021-05-27</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>不想聊就不聊了呗，摆什么臭架子，觉得自己很帅、很牛吗？作者 | 松露来源 | 新榜（ID：n...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1184</th>\n",
       "      <td>白玉兰奖公布入围名单；当当成立“公司权益保护部”；Instagram将推出短视频功能 | 猬报</td>\n",
       "      <td>2020-07-18</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：豆瓣#投其所好#你玩过B站出品的游戏吗你最看好谁？看小说，上淘宝？庆渝年最新集这是想钻...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1185</th>\n",
       "      <td>“国家队”在知乎破圈</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>“知乎在走一条很艰难的路，要优雅地面对更广大受众，展现出人民群众喜闻乐见的BCDEF面，同时...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1186</th>\n",
       "      <td>美国名流在Twitter上集体撒钱了？</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>究竟是哪里出了问题，让Twitter平台最高等级的账户被轻而易举地入侵？Twitter用户近...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1187</th>\n",
       "      <td>闯入主流世界的小众文化：为什么说“破圈”是把双刃剑？</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>“破圈是一把双刃剑，有可能打开一个新市场和带来新的客户和流量，进而带动整个平台的进一步发展，...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1188</th>\n",
       "      <td>低风险地区电影院7月20日开放营业；D站创始人被批捕；趣头条针对3.15晚会报道发道歉声明 ...</td>\n",
       "      <td>2020-07-17</td>\n",
       "      <td>http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...</td>\n",
       "      <td>图源：微博总有一个适合你所以A-Z站都有吗？道歉速度挺快会创造出更多洗脑神曲吗字节这是每个领...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1169 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                  title create_time  \\\n",
       "0                                             终于，云村要上市了  2021-05-28   \n",
       "1                                        电商内卷：“天天”都是618  2021-05-28   \n",
       "2                                   刺猬公社招聘作者，正职、见习快来快来！  2021-05-28   \n",
       "3     微信视频号直播新增重要入口；2021全球电竞运动领袖峰会议程揭晓；亚马逊宣布以84.5亿美元...  2021-05-28   \n",
       "4                                         “卑微文学”在抖音横空出世  2021-05-27   \n",
       "...                                                 ...         ...   \n",
       "1184    白玉兰奖公布入围名单；当当成立“公司权益保护部”；Instagram将推出短视频功能 | 猬报  2020-07-18   \n",
       "1185                                         “国家队”在知乎破圈  2020-07-17   \n",
       "1186                                美国名流在Twitter上集体撒钱了？  2020-07-17   \n",
       "1187                         闯入主流世界的小众文化：为什么说“破圈”是把双刃剑？  2020-07-17   \n",
       "1188  低风险地区电影院7月20日开放营业；D站创始人被批捕；趣头条针对3.15晚会报道发道歉声明 ...  2020-07-17   \n",
       "\n",
       "                                                   link  \\\n",
       "0     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "2     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "3     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "4     http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "...                                                 ...   \n",
       "1184  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1185  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1186  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1187  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "1188  http://mp.weixin.qq.com/s?__biz=MzkxNzAwMDkwNQ...   \n",
       "\n",
       "                                           content_text  \n",
       "0     专注于通过培养“云村”社区扩大用户群体及用户参与度，为长远盈利能力铺平道路。这也就是网易云的...  \n",
       "1     618起得更早了，阿里京东更焦虑了挨了182亿罚款之后，阿里比以往起得更早了。把物流推上IP...  \n",
       "2     近期，我们做出了以下一些报道作品：1、内容产业南移调查：离开北京，去杭州收割一个时代（后台回...  \n",
       "3     图源：微博是谁在监控我的618？5月26日，微信视频号更新新功能。在视频号和公众号绑定的前提...  \n",
       "4     不想聊就不聊了呗，摆什么臭架子，觉得自己很帅、很牛吗？作者 | 松露来源 | 新榜（ID：n...  \n",
       "...                                                 ...  \n",
       "1184  图源：豆瓣#投其所好#你玩过B站出品的游戏吗你最看好谁？看小说，上淘宝？庆渝年最新集这是想钻...  \n",
       "1185  “知乎在走一条很艰难的路，要优雅地面对更广大受众，展现出人民群众喜闻乐见的BCDEF面，同时...  \n",
       "1186  究竟是哪里出了问题，让Twitter平台最高等级的账户被轻而易举地入侵？Twitter用户近...  \n",
       "1187  “破圈是一把双刃剑，有可能打开一个新市场和带来新的客户和流量，进而带动整个平台的进一步发展，...  \n",
       "1188  图源：微博总有一个适合你所以A-Z站都有吗？道歉速度挺快会创造出更多洗脑神曲吗字节这是每个领...  \n",
       "\n",
       "[1169 rows x 4 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 余下为不重复的部分\n",
    "df_url_out[~df_url_out.duplicated()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 将抓取到的内容保存到本地 —— 数据输出\n",
    "with pd.ExcelWriter('{公众号}公众号链接及文章内容.xlsx'.format(公众号=公众号),mode='w',engine=\"openpyxl\") as writer:  \n",
    "            df_url_out.to_excel(writer, sheet_name=公众号)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
